{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "96201802-5384-4043-b1e3-f35dbe33566e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集数据的预测标签为： [0 1 1 0 2 2 2 0 0 2 1 0 2 1 1 0 1 1 0 0 1 1 2 0 2 1 0 0 1 2 1 2 1 2 2 0 1\n",
      " 0 1 2 2 0 1 2 1 2 0 0 0 1]\n",
      "测试集数据的真实标签为： [0 1 1 0 2 1 2 0 0 2 1 0 2 1 1 0 1 1 0 0 1 1 1 0 2 1 0 0 1 2 1 2 1 2 2 0 1\n",
      " 0 1 2 2 0 2 2 1 2 0 0 0 1]\n",
      "测试集共有50条数据，其中预测错误的数据有3条，预测准确率为0.94\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.metrics import accuracy_score\n",
    "x,y=load_iris().data,load_iris().target\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=1,test_size=50)\n",
    "model=GaussianNB()\n",
    "model.fit(x_train,y_train)\n",
    "pred=model.predict(x_test)\n",
    "print(\"测试集数据的预测标签为：\",pred)\n",
    "print(\"测试集数据的真实标签为：\",y_test)\n",
    "print(\"测试集共有%d条数据，其中预测错误的数据有%d条，预测准确率为%.2f\"%(x_test.shape[0],(pred!=y_test).sum(),accuracy_score(y_test,pred)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c8d70ac4-08b3-49b2-b21f-f85213982a35",
   "metadata": {},
   "outputs": [
    {
     "ename": "FileNotFoundError",
     "evalue": "[WinError 3] 系统找不到指定的路径。: '../db/db-ss-data/normal/'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[5], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m normalFileList\u001b[38;5;241m=\u001b[39mos\u001b[38;5;241m.\u001b[39mlistdir(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../db/db-ss-data/normal/\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      3\u001b[0m spamFileList\u001b[38;5;241m=\u001b[39mos\u001b[38;5;241m.\u001b[39mlistdir(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../db/db-ss-data/spam/\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m正常邮件的文件列表\u001b[39m\u001b[38;5;124m\"\u001b[39m,normalFileList)\n",
      "\u001b[1;31mFileNotFoundError\u001b[0m: [WinError 3] 系统找不到指定的路径。: '../db/db-ss-data/normal/'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "normalFileList=os.listdir(\"../db/db-ss-data/normal/\")\n",
    "spamFileList=os.listdir(\"../db/db-ss-data/spam/\")\n",
    "print(\"正常邮件的文件列表\",normalFileList)\n",
    "print(\"垃圾邮件的文件列表\",spamFileList)\n",
    "stopList=[]\n",
    "for line in open(\"../db/db-ss-data/stopwords.txt\",encoding='utf-8'):\n",
    "    stopList.append(line[:len(line)-1])\n",
    "print(\"停用词文件内容\",stopList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8556c8e-bb28-4d5c-8ca8-3a8c8b936694",
   "metadata": {},
   "outputs": [],
   "source": [
    "from jieba iimport cut\n",
    "from re importsub\n",
    "def getWords(file,stopList):\n",
    "    wordsList=[]\n",
    "    for line in open(file,encoding='utf-8'):\n",
    "        line=line.strip()\n",
    "        line=sub()r'[.【】0-9、--，。！\\~*]','',line'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
